﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class NhomTKKeToan : MarshalByRefObject
    {
        private static SqlCommand cmd;
        private static ArrayList arr;
        private static SqlDataReader dr;
        private static SqlConnection cn;
        public NhomTKKeToan() 
        {
            cmd = null;
            arr = null;
            dr = null;
            cn = null;
        }

        public static Entities.NhomTKKeToan[] Select(Entities.NhomTKKeToan km)
        {
            Entities.NhomTKKeToan[] list = null;
            try
            {
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.NhomTKKeToan.sp_Select_NhomTKKeToan, cn);
                cmd.Parameters.Add(Common.Constants.NhomTKKeToan.HanhDong, SqlDbType.NVarChar,50).Value = km.HanhDong;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.NhomTKKeToan row = new Entities.NhomTKKeToan();
                    row.NhomTKKeToanID = int.Parse(dr[Common.Constants.NhomTKKeToan.NhomTKKeToanID].ToString());
                    row.MaNhomTKKeToan = dr[Common.Constants.NhomTKKeToan.MaNhomTKKeToan].ToString();
                    row.TenNhomTaiKhoan = dr[Common.Constants.NhomTKKeToan.TenNhomTaiKhoan].ToString();
                    row.GhiChu = dr[Common.Constants.NhomTKKeToan.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.NhomTKKeToan.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.NhomTKKeToan[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.NhomTKKeToan)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
                
            }
            return list;
        }

        public static int InsertUpdate(Entities.NhomTKKeToan km)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(Common.Constants.NhomTKKeToan.sp_InsertUpdate_NhomTKKeToan, cn);
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.HanhDong, SqlDbType.NVarChar).Value = km.HanhDong;
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.NhomTKKeToanID, SqlDbType.Int).Value = km.NhomTKKeToanID;
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.MaNhomTKKeToan, SqlDbType.VarChar).Value = km.MaNhomTKKeToan;
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.TenNhomTaiKhoan, SqlDbType.NVarChar).Value = km.TenNhomTaiKhoan;
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.GhiChu, SqlDbType.NVarChar).Value = km.GhiChu;
                    cmd.Parameters.Add(Common.Constants.NhomTKKeToan.Deleted, SqlDbType.Bit).Value = km.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    if (rt != 0)
                    {
                        if (km.HanhDong == Common.Constants.Insert)
                        {
                            LuuVietHeThong.LuuViet
                                (
                                "Thêm",
                                km.TaiKhoan.MaNhanVien,
                                "Thêm nhóm tài khoản kế toán " + km.MaNhomTKKeToan,
                                km.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                        else
                        {
                            LuuVietHeThong.LuuViet
                                (
                                "Sửa",
                                km.TaiKhoan.MaNhanVien,
                                "Sửa nhóm tài khoản kế toán " + km.MaNhomTKKeToan,
                                km.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                    }
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }

        public static int Delete(Entities.NhomTKKeToan km)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.NhomTKKeToan.sp_Delete_NhomTKKeToan, cn);
                cmd.Parameters.Add(Common.Constants.NhomTKKeToan.NhomTKKeToanID, SqlDbType.Int).Value = km.NhomTKKeToanID;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    LuuVietHeThong.LuuViet
                        (
                        "Xóa",
                        km.TaiKhoan.MaNhanVien,
                        "Xóa nhóm tài khoản kế toán " + km.MaNhomTKKeToan,
                        km.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToShortDateString()
                        );
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
#endregion